package com.example.mylibrarybase.yumengmeng11.utils.popwindowUtils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;

import java.util.ArrayList;


/**
 * Created by 35876 于萌萌
 * 创建日期: 22:16 . 2016年07月06日
 * 描述:popWindow的工具类
 * <p/>
 * <p/>
 * 备注:
 */
public class PopWindowUtils_11 {
    //

    static ArrayList<String> mArrayList;
    private static ListView lv_listview;

    /**
     *
     * 弹出 popwindow
     * @param context 上下文
     * @param mStrings window 里面的数据
     * @param view 需要添加的pop
     * @param num1 左右x偏移量1
     * @param num2 上下y偏移量2
     * @param activity 你需要变色的activity 可以为空
     * @param callback 回调
     */
    public static PopupWindow showPopupWindow(final Context context, ArrayList<String> mStrings, View view, int num1, int num2, final Activity activity, final Callback callback){
        //背景变色
        backgroundAlpha(activity,0.7f);


        mArrayList = new ArrayList<>();
       // 一个自定义的布局，作为显示的内容
       View contentView = LayoutInflater.from(context).inflate(
               R.layout.item_popwindow_11, null);
       for (int i = 0; i < mStrings.size(); i++) {
           mArrayList.add(mStrings.get(i));
       }
        lv_listview = (ListView) contentView.findViewById(R.id.lv_listview);

     PopwindowList_11 popwindowList =  new PopwindowList_11(context, mArrayList);
       lv_listview.setAdapter(popwindowList);
       // 菜单项点击监听器
        final PopupWindow popupWindow = new PopupWindow(contentView,
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setFocusable(true);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
              LogUtils.i("我现在已经关闭了");

                if (activity!=null){
                    WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                    lp.alpha = 1f; //0.0-1.0
                    activity.getWindow().setAttributes(lp);
                    callback.getWindow(popupWindow);
                }

            }
        });
        lv_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = mArrayList.get(position);
                callback.getString(s,position);
                popupWindow.dismiss();
            }
        });
       popupWindow.setTouchable(true);
       popupWindow.setTouchInterceptor(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               Log.i("mengdd", "onTouch : ");
               return false;
               // 这里如果返回true的话，touch事件将被拦截
               // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
           }
       });

       // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
       // 我觉得这里是API的一个bug
       popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
               R.drawable.anquan_11));

       // 设置好参数之后再show
       popupWindow.showAsDropDown(view,num1,num2);
        return popupWindow;

   }

    public interface Callback {
        void getString(String itemName, int postion);

        void getWindow(PopupWindow popupWindow);

    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity activity,float bgAlpha)
    {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }
}



